<?php

namespace app\common\model;

use think\Model;

class Setting extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    protected $append = [
    ];

    /**
     * 获取器: 转义数组格式
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }

    /**
     * 获取指定项设置
     * $id 模板或小程序ID
     * $setting_type 10=小程序设置，20模板设置
     */
    public static function getItem($key,$id='',$setting_type=20)
    {
        if($setting_type==20){
            return self::where([
                'setting_type' => $setting_type,
                'key' => $key,
                'formwork_order_id' => $id
            ])->find();
        }
        return self::where([
            'setting_type' => $setting_type,
            'key' => $key,
            'applet_id' => $id
        ])->find();
    }

}
